// pages/member/coupon/index.js
var app = getApp(),
  core = app.requirejs('core'),
  order = app.requirejs('biz/order');
var diyad = app.requirejs('biz/ad');
var util = require('../../../utils/util.js')
const api = require("../../../utils/api.js")
var image_url = 'https://totolestorage.blob.core.chinacloudapi.cn/totoleec/xcx/coupon/'
Page({
  data: {
    catelists: [{
      url: image_url + "tag-1-2.png",
      url_ico: image_url + "tag-1-1.png",
      coupons: image_url + "coupon-1.png",
      id: 0
    }, {
      url: image_url + "tag-2-2.png",
      url_ico: image_url + "tag-2-1.png",
      coupons: image_url + "coupon-2.png",
      id: 1
    }, {
      url: image_url + "tag-3-2.png",
      url_ico: image_url + "tag-3-1.png",
      coupons: image_url + "coupon-3.png",
      id: 2
    }],
    currentTab: 0,
    memberCoupon: {
      noCouponList: [],
      alCouponList: [],
      exCouponList: [],
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let $this = this
    let apicontent = api.markings.selectUserCoupon()
    let memberCouponList = []
    // let isShow = $this.data.isShow
    core.post(apicontent, {

    }, function (res) {


      console.log(res);
      res.noCouponList.forEach(e => {
        e.startTime = $this.timeFun(e.startTime).split(' ')[0]
        e.endTime = $this.timeFun(e.endTime).split(' ')[0]
      });
      res.alCouponList.forEach(e => {
        e.startTime = $this.timeFun(e.startTime).split(' ')[0]
        e.endTime = $this.timeFun(e.endTime).split(' ')[0]
      });
      res.exCouponList.forEach(e => {
        e.startTime = $this.timeFun(e.startTime).split(' ')[0]
        e.endTime = $this.timeFun(e.endTime).split(' ')[0]
      });
      $this.setData({
        memberCoupon: res
      })

      // if (res.noCouponList.length > 0) {
      //   isShow[0] = 1
      // }
      // if (res.alCouponList.length > 0) {
      //   isShow[1] = 1

      // }
      // if (res.exCouponList.length > 0) {
      //   isShow[2] = 1
      // }


      // memberCouponList.push(res.noCouponList)
      // memberCouponList.push(res.alCouponList)
      // memberCouponList.push(res.exCouponList)
      // memberCouponList.forEach(e => {
      //   if (e.length > 0) {
      //     e.forEach(ele => {
      //       ele.startTime = $this.timeFun(ele.startTime).split(' ')[0]
      //       ele.endTime = $this.timeFun(ele.endTime).split(' ')[0]
      //     })
      //   }
      // })
      // $this.setData({
      //   memberCouponList,
      //   isShow
      // })
    })
  },
  //切换tab
  tabNav: function (e) {
    let tabid = e.target.dataset.id
    this.setData({
      currentTab: tabid,
    }),
      /**启动广告 */
      diyad.get(this, function (res) {


      })
  },
  // 格式化时间
  timeFun(data) {

    var time = new Date(data)
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    var h = time.getHours() < 10 ? '0' + time.getHours() : time.getHours();
    var mm = time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes();
    var s = time.getSeconds() < 10 ? '0' + time.getSeconds() : time.getSeconds();

    return y + '.' + m + '.' + d + ' ' + h + ':' + mm + ':' + s;
  },
  timeStamp(index) {
    if (index != '') {
      let time = new Date(index).getTime()
      return time
    }

    return ''
  },
  // 到秒
  timeStampFun() {
    let time = Date.parse(new Date())
    return time
  },

  //使用优惠卷跳转
  usecouponschange() {
    let id = this.data.currentTab
    if (id == 0) {
      let cateid = 4
      app.setCache("cateid", cateid)
      wx.switchTab({
        url: '/pages/goods/index/index'
      })
    } else if (id == 1) {
      wx.showToast({
        title: '优惠卷已使用',
        icon: 'none',
        duration: 2000
      })
    } else if (id == 2) {
      wx.showToast({
        title: '优惠卷已失效',
        icon: 'none',
        duration: 2000
      })
    }
  },
})